# -*- coding: utf-8 -*-
#
# Author:: hanjunfeng
# Date:: 2022/7/7
from lib import ths_
from lib.indicator import get_ma_list


def get_gains_combo(candles_data):
    count = 0
    candles_data_len = len(candles_data)
    if candles_data_len > 0:
        if candles_data[0][7] >= 0:
            for i in range(candles_data_len - 1):
                if candles_data[i][7] < 0:
                    break
                count += 1
        else:
            for i2 in range(candles_data_len - 1):
                if candles_data[i2][7] > 0:
                    break
                count -= 1
        return count
    else:
        return count


def get_ma_up_combo(ma_list):
    count = 0
    if len(ma_list) > 1:
        ma_list_len = len(ma_list)
        if ma_list[0] - ma_list[1] >= 0:
            for i in range(ma_list_len - 1):
                if ma_list[i] - ma_list[i + 1] < 0:
                    break
                count += 1
        else:
            for i2 in range(ma_list_len - 1):
                if ma_list[i2] - ma_list[i2 + 1] > 0:
                    break
                count -= 1
        return count
    else:
        return count


def get_golden_cross_combo(ma_list_small, ma_list_big):
    ma_list_small_len = len(ma_list_small)
    ma_list_big_len = len(ma_list_big)
    if ma_list_big_len > ma_list_small_len:
        ma_len = ma_list_small_len
    else:
        ma_len = ma_list_big_len

    count = 0
    if ma_len > 0:
        if ma_list_small[0] - ma_list_big[0] >= 0:
            for i in range(ma_len):
                if ma_list_small[i] - ma_list_big[i] < 0:
                    break
                count += 1
        else:
            for i2 in range(ma_len):
                if ma_list_small[i2] - ma_list_big[i2] > 0:
                    break
                count -= 1
        return count
    else:
        return count


def get_golden_cross_socre(ma_list_small, ma_list_big, length=10):
    ma_list_small_len = len(ma_list_small)
    ma_list_big_len = len(ma_list_big)
    if ma_list_big_len > ma_list_small_len:
        ma_len = ma_list_small_len
    else:
        ma_len = ma_list_big_len

    if ma_len < length:
        length = ma_len

    count = 0
    if length > 0:
        for i in range(length):
            if ma_list_small[i] - ma_list_big[i] > 0:
                count += 1
        return count
    else:
        return count


def get_throught_up_boll_times(candles_data, boll_list, length=10):
    len1 = len(candles_data)
    len2 = len(boll_list)
    if len1 > len2:
        length_tmp = len2
    else:
        length_tmp = len1

    if length_tmp < length:
        length = length_tmp

    count = 0
    for i in range(length):
        high_price = candles_data[i][2]
        boll_up = boll_list[i][1]
        if high_price > boll_up:
            count += 1

    return count


if __name__ == "__main__":
    # from lib.indicator import get_ma_list, get_boll_list
    # # from lib.ts_ import get_candles_data
    #
    # # ts_code = "300750.SZ"
    # ts_code = "300879.SZ"
    # ts_code = "002585.SZ"
    #
    # candles_data = get_candles_data(ts_code)
    # ma_list_type3 = get_ma_list(candles_data, ma_type=3, offset=0)
    # ma_list_type15 = get_ma_list(candles_data, ma_type=15, offset=0)
    # boll_list_type15 = get_boll_list(candles_data, mid=15, offset=0)
    #
    # print(ma_list_type3)
    # print(ma_list_type15)
    # print(candles_data)

    ths_code3 = "sz_399006"
    ths_code3 = "sz_159613"
    candles_data = ths_.get_candles_data(ths_code3)
    ma_list_type3 = get_ma_list(candles_data, ma_type=3, offset=0)

    def test_get_ma_up_combo():
        # ma_list_type3 = [1, 2, 3]
        # ma_list_type3 = [3, 2, 1]
        print(candles_data)
        print(ma_list_type3)
        res = get_ma_up_combo(ma_list_type3)
        # res = get_ma_up_combo(ma_list_type15)
        print(res)

    def test_get_gains_combo():
        res = get_gains_combo(candles_data)
        print(res)

    def test_get_golden_cross_combo():
        res = get_golden_cross_combo(ma_list_type3, ma_list_type15)
        print(res)

    def test_get_golden_cross_socre():
        res = get_golden_cross_socre(ma_list_type3, ma_list_type15)
        print(res)

    def test_get_throught_up_boll_times():
        res = get_throught_up_boll_times(candles_data, boll_list_type15)
        print(res)

    test_get_ma_up_combo()
